package com.spoto.oa.servlet;

import java.io.IOException;
import java.sql.Date;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.spoto.oa.dao.impl.SalesDaoImpl;
import com.spoto.oa.model.Employee;
import com.spoto.oa.model.Product;
import com.spoto.oa.model.Sales;
import com.spoto.oa.service.SalesServ;
import com.spoto.oa.service.impl.ProductServImpl;
import com.spoto.oa.service.impl.SalesServImpl;
import com.spoto.oa.util.DateUtil;

public class SalesServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public SalesServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Sales sales = new Sales();
		List<Sales> salesList = null;
		SalesDaoImpl salesDao = new SalesDaoImpl();
		HttpSession session = request.getSession();
		Product product = null;
		ProductServImpl proServ = new ProductServImpl();
		SalesServ salesServ = new SalesServImpl();
		String op_type = request.getParameter("op_type");
		Employee loginEmployee = (Employee) session.getAttribute("login_emp");
		boolean role=true;
//		boolean role = ((loginEmployee.getIsAdmin().equals("是")) ? true : false);
		if (op_type.equals("add")) {
			product = proServ.selectByCode(request.getParameter("proCode"));
			sales.setNumbers(Integer.parseInt(request.getParameter("numbers")));
			sales.setProduct(product);
			sales.setCreator((String) session.getAttribute("username"));
			sales.setDate(DateUtil.nowDate());
			sales.setRealPrice(Float.parseFloat(request
					.getParameter("realPrice")));
			sales.setRemarks(request.getParameter("remarks"));
			sales.setMonth(Calendar.getInstance().get(Calendar.MONTH + 1));
			try {
				salesDao.addSales(sales);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			salesList = salesServ.getSales("", "", loginEmployee.getUsername(),
					role);
			// salesList = salesServ.getSales("", "", "Young", true);
			request.setAttribute("salesList", salesList);
			request.getRequestDispatcher("/sales/welcome_sales.jsp").forward(
					request, response);
		} else if (op_type.equals("search")) {
			String searchType = request.getParameter("searchType");
			String infor = request.getParameter("infor");			
			if (searchType.equals("date")) {
				Date dateBegin = DateUtil.parseToDate(request
						.getParameter("dateBegin"));
				Date dateEnd = DateUtil
						.parseToDate(request.getParameter("dateEnd"));
				salesList = salesServ.getSalesByTime(dateBegin, dateEnd, "Young", true);
//				salesList = salesServ.getSalesByTime(dateBegin, dateEnd,
//						loginEmployee.getUsername(), role);
			} else {
//				salesList = salesServ.getSales(searchType, infor, loginEmployee
//						.getUsername(), role);
				salesList = salesServ.getSales(searchType, infor, "Young", true);
			}
			
			request.setAttribute("infor", infor);
			request.setAttribute("searchType", searchType);
			request.setAttribute("salesList", salesList);
			request.getRequestDispatcher("/sales/welcome_sales.jsp").forward(
					request, response);
		} else if (op_type.equals("invalid")) {
			int id = Integer.parseInt(request.getParameter("id"));
			salesServ.setInvalid(id);
			salesList = salesServ.getSales("", "", "Young", true);
			request.setAttribute("salesList", salesList);
			request.getRequestDispatcher("/sales/welcome_sales.jsp").forward(
					request, response);
			request.setAttribute("salesList", salesList);
			request.getRequestDispatcher("/sales/welcome_sales.jsp").forward(
					request, response);
		} else if (op_type.equals("multi_invalid")) {
			String[] invalidId = request.getParameterValues("invalid-flag");
			for (int i = 0; i < invalidId.length; i++) {
				salesServ.setInvalid((Integer.parseInt(invalidId[i])));
			}
			salesList = salesServ.getSales("", "", "Young", true);
			request.setAttribute("salesList", salesList);
			request.getRequestDispatcher("/sales/welcome_sales.jsp").forward(
					request, response);
		} else if (op_type.equals("to_update")) {
			int id = Integer.parseInt(request.getParameter("id"));
			sales = salesDao.getSalesById(id);
			request.setAttribute("sales", sales);
			if ("to_update".equals(op_type)) {
				request.getRequestDispatcher("/sales/update_sales.jsp")
						.forward(request, response);
			}
		} else if (op_type.equals("update")) {
			int id = Integer.parseInt(request.getParameter("id"));
			product = proServ.selectByCode(request.getParameter("proCode"));
			sales = salesDao.getSalesById(id);
			sales.setNumbers(Integer.parseInt(request.getParameter("numbers")));
			sales.setProduct(product);
			sales.setCreator((String) session.getAttribute("username"));
			sales.setRealPrice(Float.parseFloat(request
					.getParameter("realPrice")));
			sales.setRemarks(request.getParameter("remarks"));
			salesServ.updateSales(id, sales);
			salesList = salesServ.getSales("", "", "Young", true);
			request.setAttribute("salesList", salesList);
			request.getRequestDispatcher("/sales/welcome_sales.jsp").forward(
					request, response);
		}

	}
}
